const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");

var reqUrl = "http://nodejs.cn/";

//可以将url字符串转换成对象的结构
var obj = url.parse(reqUrl);

var options = {
    hostname:'nodejs.cn',
    port:80,
    path:'/',
    method:'GET'
}

var request = http.request(options,(res)=>{
    var str = '';
    res.on("data",(chunk)=>{
        str += chunk;
    })
    res.on("end",()=>{
        var filePath = path.join(__dirname,"/index.html");
        fs.writeFileSync(filePath,str);
    })
})
//http.request方法一定要调用end方法
request.end();